<?php

namespace app\admin\command;

use app\admin\command\Api\library\Builder;


use app\admin\model\SpiderAiqiyi;
use app\admin\model\SpiderAiqiyiCollection;
use app\admin\model\SpiderAiqiyiLog;
use app\admin\model\SpiderTengxun;
use app\admin\model\SpiderTengxunCollection;
use app\admin\model\SpiderTengxunLog;
use app\admin\model\SpiderYouku;
use app\admin\model\SpiderYoukuCollection;
use app\admin\model\SpiderYoukuLog;
use GuzzleHttp\Cookie\CookieJar;
use phpspider\core\phpspider;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use phpspider\core\requests;
use phpspider\core\selector;
use think\Model;

class Spider2 extends Command
{
    protected function configure()
    {
        /* Do NOT delete this comment */
        /* 不要删除这段注释 */
        $this
            ->setName('spider2')   //腾讯视频抓包
            ->addOption('type', 't', Option::VALUE_REQUIRED, 'type', null)
            ->setDescription("spider 抓包");

    }

    protected function execute(Input $input, Output $output)
    {
        $type = $input->getOption('type');
        //初始更新
        if(empty($type)){
            $list = SpiderTengxun::select();
            if(empty($list)) return true;
            foreach ($list as $val){
                $url = $val["url"];
                $parent_id = $val["id"];
                if(empty($url)) continue;
                if(strpos($url,"?")!==false){
                    $url.="&userId=".time();
                }else{
                    $url.="?userId=".time();
                }
                try{
                    $content = requests::get($url);
                    $play = selector::select($content, "//div[@class='action_item action_count']");//播放量
                    $update_date = selector::select($content, "//div[@class='mod_player_side']");//更新日期
                    $play = selector::select($play,"//span[@class='icon_text']");
                    $play = strip_tags($play);
                    $play_number = $this->play_number($play);
                    $update_date = selector::select($update_date, "//div[@class='player_hint']");// 每周六10点更新一集
                    $preg = "/(每周.+?)'/";
                    preg_match_all($preg,$update_date,$data);
                    $update_date = $data[1][0]??"";

                    $comment_id = selector::select($content, "#\"comment_id\"?:\"(.+?)\",#","regex");//  comment_id
                    $cover_id = selector::select($content, "#\"cover_id\"?:\"(.+?)\",#","regex");  //  cover_id
                    $comment_number = $this->comment_number($comment_id);
                    $update_data = [
                        "play"=>(string)trim($play),
                        "play_number"=>$play_number,
                        "update_date"=>$update_date,
                        "comment_number"=>$comment_number,
                        "barrage_number"=>0,
                        "current_collection_number"=>0,
                        "cover_id"=>$cover_id,
                    ];
                    SpiderTengxun::update($update_data,['id'=>$parent_id]);
                    //更新集数
                    $this->collection_list($cover_id,$parent_id);
                }catch (\Exception $e){
                     throw $e;
                }
            }
            return true;
        }

        //更新集数  一天一次
        if($type=="list"){
            $list = SpiderTengxun::select();
            foreach ($list as $val){
                $parent_id = $val["id"];
                $cover_id = $val["cover_id"];
                if(empty($cover_id)) continue;
                $this->collection_list($cover_id,$parent_id);
            }
        }

        //更新集的热度，评论数，弹幕数 一小时一次
        if($type=="date"){
            $list = SpiderTengxun::select();
            try{
                foreach ($list as $val){
                    $url = $val["url"];
                    $parent_id = $val["id"];
                    $cover_id = $val["cover_id"];
                    if(empty($url) || empty($cover_id)) continue;
                    if(strpos($url,"?")!==false){
                        $url."=&userId=".time();
                    }else{
                        $url.="?userId=".time();
                    }
                    $content = requests::get($url);
                    $play = selector::select($content, "//div[@class='action_item action_count']");//播放量
                    $update_date = selector::select($content, "//div[@class='mod_player_side']");//更新日期
                    $play = selector::select($play,"//span[@class='icon_text']");
                    $play = strip_tags($play);
                    $play_number = $this->play_number($play);
                    $update_date = selector::select($update_date, "//div[@class='player_hint']");// 每周六10点更新一集
                    $preg = "/(每周.+?)'/";
                    preg_match_all($preg,$update_date,$data);
                    $update_date = $data[1][0]??"";

                    $collection_list = SpiderTengxunCollection::where("parent_id",$parent_id)->select();
                    foreach ($collection_list as $val){
                        $comment_id = $val["comment_id"];
                        if(empty($comment_id)) continue;
                        $comment_number = $this->comment_number($comment_id);
                        SpiderTengxunCollection::update(["comment_number"=>$comment_number],["id"=>$val["id"]]);
                    }

                    $total_comment_number = SpiderTengxunCollection::where("parent_id",$parent_id)->sum("comment_number");
                    $update_data = [
                        "play"=>(string)trim($play),
                        "play_number"=>$play_number,
                        "update_date"=>$update_date,
                        "comment_number"=>$total_comment_number,
                    ];
                    SpiderTengxun::update($update_data,['id'=>$parent_id]);

                    //日志
                    $date = date("Y-m-d");
                    $log_model = SpiderTengxunLog::where("parent_id",$parent_id)->where("date",$date)->find();
                    if(empty($log_model)){
                        $log_model = new SpiderTengxunLog();
                    }

                    $yesterday_time = strtotime("-1 day");
                    $yesterday_date = date("Y-m-d",$yesterday_time);
                    $yesterday_log_model = SpiderTengxunLog::where("parent_id",$parent_id)->where("date",$yesterday_date)->find();

                    $increment_play_number = 0;
                    if($yesterday_log_model){
                        $increment_play_number = sprintf("%.2f",$play_number - $yesterday_log_model["play_number"]);
                    }

                    $log=[
                        "comment_number"=>$total_comment_number,
                        "date"=>$date,
                        "parent_id"=>$parent_id,
                        "play"=>(string)trim($play),
                        "play_number"=>$play_number,
                        "increment_play_number"=>$increment_play_number,
                        "createdtime"=>time()
                    ];
                    $log_model->save($log);
                }
            }catch (\Exception $e){
                throw $e;
            }
            return true;
        }
    }

    //获取单集的评论数
    public function comment_number($comment_id){
        $url="https://video.coral.qq.com/article/".$comment_id."/commentnum";
        $json = requests::get($url);
        $json_ar = json_decode($json,true);
        $comment_number = $json_ar['data']["commentnum"]??0;
        return $comment_number;
    }

    //集数更新
    public function collection_list($cid,$parent_id){
        $url = "https://pbaccess.video.qq.com/trpc.universal_backend_service.page_server_rpc.PageServer/GetPageData?video_appid=3000010&vplatform=2";
        $data = [
            "page_params"=>[
                "req_from"=>"web",
                "id_type"=>"1",
                "page_type"=>"detail_operation",
                "page_id"=>"vsite_episode_list",
                "cid"=>$cid,
                "page_size"=>"500"
            ],
        ];
        try{
            $httpClient = new \GuzzleHttp\Client();
            $headers = ["Referer"=>"https://v.qq.com/"];
            $cookies = [
                "vversion_name"=>"8.2.95"
            ];
            $cookieJar = CookieJar::fromArray($cookies,"pbaccess.video.qq.com");
            $response = $httpClient->request("post",$url,[
                'headers'=>$headers,
                "json"=>$data,
                "form_params"=>$data,
                "cookies"=>$cookieJar
            ]);
            $content = $response->getBody()->getContents();
            $data = json_decode($content,true);

            if(empty($data) || !is_array($data)) return false;
            $list = $data["data"]["module_list_datas"][0]["module_datas"][0]["item_data_lists"]["item_datas"]??[];
            if(empty($list) || !is_array($list)) return false;

            $collection_list = SpiderTengxunCollection::where("parent_id",$parent_id)->select();
            $vid_id_ar = $collection_list?array_column($collection_list,"vid"):[];

            $max_jishu = 0;
            $collection_model = new SpiderTengxunCollection();
            foreach ($list as $list_val){
                if(empty($list_val["item_params"]) || !is_array($list_val["item_params"])){
                    continue;
                }
                $vid = trim($list_val["item_params"]["vid"]);
                $url = "https://v.qq.com/x/cover/".$cid."/".$vid.".html";
                $collection_number = (int)$list_val["item_params"]["title"];
                if($collection_number<1){
                    $collection_number = (int)$list_val["item_params"]["c_title_output"];
                }
                $collection_name = trim($list_val["item_params"]["play_title"]??"");
                $collection_desc = trim($list_val["item_params"]["union_title"]??"");
                if($max_jishu < $collection_number && strpos($collection_desc,"预告") === false){
                    $max_jishu = $collection_number;
                }


                if(!in_array($vid,$vid_id_ar)){
                    $content = requests::get($url);
                    $comment_id = selector::select($content, "#\"comment_id\"?:\"(.+?)\",#","regex");//  comment_id
                    $data = [
                        "comment_id"=>$comment_id,
                        "vid"=>$vid,
                        "parent_id"=>$parent_id,
                        "url"=>$url,
                        "collection_number"=>$collection_number,
                        "collection_name"=>$collection_name,
                        "collection_desc"=>$collection_desc,
                    ];
                    $model = clone $collection_model;
                    $model->save($data);
                }
            }
            SpiderTengxun::update(["current_collection_number"=>$max_jishu],["id"=>$parent_id]);
            return true;
        }catch (\GuzzleHttp\Exception\RequestException $e){
            throw $e;
        }
        return false;
    }

    //转化
    public function play_number($play){
        $number = (float)$play;
        if(strpos($play,"亿") !== false){
            $number = $number * 10000;
        }elseif(strpos($play,"万") !== false){
            $number = sprintf("%.2f",$number);
        }elseif(strpos($play,"千") !== false){
            $number = $number * 0.1;
            $number = sprintf("%.2f",$number);
        }elseif(strpos($play,"百") !== false){
            $number = $number * 0.01;
            $number = sprintf("%.2f",$number);
        }
        return sprintf("%.2f",$number);
    }

}
